##
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
##

#
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
#
#

# raw note means the draft release content copied down which is generated by Github workflow
# this script helps add the URLs to all PR and have the right release note filename

import os
import sys
import fileinput
import re

link_prefix = "https://github.com/opensearch-project/dashboards-reports/tree/main/reports-scheduler"
searchExp = re.compile("([\(\[]).*?([\)\]])")

current_date = raw_input("what day is today (e.g. 2020-06-29): ")
file_path = raw_input("Path to raw note file (e.g., note.md): ")
plugin_name = "reports-scheduler"
plugin_version = raw_input('Plugin version (x.x.x.x): ')

app_num = int(
    raw_input('OpenSearch plugin (enter 1) or Kibana plugin (enter 2)? '))
app = 'OpenSearch'
if app_num is 2:
    app = 'OpenSearch-Dashboards'

app_version = raw_input(app + ' compatibility version (x.x.x): ')

for line in fileinput.input(file_path, inplace=True):
    # Add title and ES/Kibana compatibility
    if fileinput.isfirstline():
        line = "## Version " + plugin_version + " " + current_date + "\n\n" \
            "Compatible with " + app + " " + app_version + "\n"

    # Add link to PRs
    if '*' in line:
        start = line.find('#') + 1
        end = line.find(')', start)
        pr_num = line[start:end]
        line = re.sub(searchExp, "([#" + pr_num +
                      "](" + link_prefix + pr_num + "))", line)
    sys.stdout.write(line)

# Rename file to be consistent with OpenSearch standards
new_file_path = "opensearch-" + plugin_name + ".release-notes-" + \
    plugin_version + ".md"
os.rename(file_path, new_file_path)

print('\n\nDone!\n')
